# -*- coding: utf-8 -*-
import os
import pandas as pd
import numpy as np

def prep_data(infiles,outfiles=[],params=[]):
	# raw.csv:
	#'ID',		ID
	#'QHSJ', 	起火时间
	#'QHCS',	起火场所
	#'ZJYY',	火灾原因
	#'CCSS',	财产损失（与直接财产损失相同）
	#'GHMJ',	过火面积
	#'ZJJJSS'	直接财产损失

	a  = pd.read_csv(infiles[0],index_col=0,dtype={'QHCS':str,'ID':str,'ZJYY':str}) 
	ad = pd.read_csv(infiles[1],names=['ID','zip','addr'],dtype={'ID':str,'zip':str})
	xy = pd.read_csv(infiles[2],dtype={'ID':str,'zip':str})	
	
	# merge two tables
	a=pd.merge(a,ad[['ID','zip','addr']],on='ID',how='left')
	a=pd.merge(a,xy[['ID','lat','lng']],on='ID',how='left')
		
	# process time info
	a.QHSJ = pd.to_datetime(a.QHSJ)
	a.loc[:,'month']=a.QHSJ.dt.month
	a.loc[:,'yr']=a.QHSJ.dt.year
	a.loc[:,'yQ']=a.yr*100+a.QHSJ.dt.quarter
	a.loc[:,'yM']=a.yr*100+a.QHSJ.dt.month
	a.loc[:,'city']=a.zip.str[:4]
	a.loc[:,'hour']=a.QHSJ.dt.hour

	# hist,bins = np.histogram(a.GHMJ,bins=range(0,100,1),range=(0,100))
	a.loc[:,'ghmj_hist']=a.GHMJ
	a[a.ghmj_hist>100]=100

	# hist,bins = np.histogram(a.ZJJJSS,bins=range(0,2500,20),range=(0,2500))
	a.loc[:,'jjss_hist']=a.ZJJJSS/20
	a[a.jjss_hist>125]=125

	# a.loc[:,'type']=a[typestr].str[:2]

	# a.lat.fillna(-1,inplace=True)
	# a.lng.fillna(-1,inplace=True)
	# a.GHMJ.fillna(-1,inplace=True)
	# a.ZJJJSS.fillna(-1,inplace=True)
	# a.city.fillna(-1,inplace=True)
	a.fillna(-1,inplace=True)
	a.city=a.city.astype(int)
	
	a.to_csv(outfiles[0])